<template>
  <div class="outpatient-container">
    <div class="outpatient-left">
      <patient-list
        ref="patientList"
        @quick-seeDoctor="quickSeeDoctor"
        @list-click="listClick"
      ></patient-list>
    </div>
    <div class="outpatient-right">
      <case-history
        ref="caseHistory"
        @caseSuccess="caseSuccess"
        :medicalType="medicalType"
        :registrationItem="registrationItem"
      ></case-history>
    </div>
  </div>
</template>
<script>
import {
  patientDetailByPatientId,
  patientDetailByRegisterId,
} from "@/api/outpatient.js";
export default {
  name: "outpatient",
  components: {
    patientList: () => import("./modules/patientList"),
    caseHistory: () => import("./modules/caseHistory"),
  },
  data() {
    return {
      medicalType: 1, // 1,快速就诊，2，挂号后的普通就诊, 3,患者补病例 ,4,患者新增病例(传1)
      registrationItem: null,
      coprdata: null,
      patientId: 0,
      registerid: 0,
      type: 0,
      userInfo: null,
    };
  },
  created() {
    console.log(this.QINIU);
    this.userInfo = this.$store.getters.userInfo;
    this.registerid = Number(this.$route.query.registerid);
    // // 患者栏目
    this.type = Number(this.$route.query.type);
    let id = Number(this.$route.query.id);

    if (id) {
      this.patientId = id;
      this.getPatientDetailByPatientId(this.patientId);
    }
    if (this.registerid) {
      this.getPatientDetailByRegisterId(this.registerid);
    }
  },
  methods: {
    // 快速就诊
    quickSeeDoctor() {
      console.log("快速就诊");
      this.medicalType = 1;
      this.registrationItem = null;
      try {
        this.$refs.caseHistory.$refs.caseContent.nameInputFocus();
      } catch (err) {
        console.log(err, "姓名获得焦点失败");
      }
    },
    listClick(item) {
      console.log(item);
      this.coprdata = item;
      this.getPatientDetailByRegisterId(item.id);
    },
    caseSuccess(medicalType, caseData) {
      this.medicalType = medicalType;
      if (this.medicalType == 1||this.medicalType == 4) {
        this.registerid = caseData.register_id;
        this.$refs.patientList.itemId = caseData.register_id;
        this.$refs.patientList.isHighLight = true;
        this.getPatientDetailByRegisterId(caseData.register_id);
        this.$router.push({
          name: "outpatient",
          query: {
            registerid: caseData.register_id,
          },
        });
      }else if(this.medicalType==3){
        this.medicalType = 1;
        this.registrationItem = null;
      }
      this.$refs.patientList.refreshData();
      // 病例创建成功
      if (this.registrationItem) {
        this.registrationItem.register_status = 3;
      }
      
    },
    getPatientDetailByPatientId(patientId) {
      patientDetailByPatientId(patientId).then((res) => {
        if (res.code == 200) {
          this.registrationItem = res.data;
          this.registrationItem.resume_id = res.data.ID;
          this.registrationItem.doctor_id = this.userInfo.id;
          this.registrationItem.doctor_name = this.userInfo.real_name;
          this.registrationItem.department_name = this.userInfo.department
            ? this.userInfo.department.DepartmentName
            : "";
          if (this.registrationItem.age) {
            this.registrationItem.age = this.registrationItem.age
              .split(",")
              .map((value) => {
                return Number(value);
              });
          }
          if (this.type) {
            if (this.type == 1) {
              this.medicalType = 4;
            } else if (this.type == 3) {
              this.medicalType = 3;
            }
          }
        }
      });
    },
    getPatientDetailByRegisterId(registerid) {
      var that = this;
      patientDetailByRegisterId({ id: registerid }).then((res) => {
        if (res.code == 200) {
          let datas = res.data
          datas.tel = res.data.contact;
          if (datas.age) {
            datas.age = datas.age
              .split(",")
              .map((value) => {
                return Number(value);
              });
          }
          that.registrationItem = datas;
        } else {
          that.registrationItem = that.coprdata;
        }
        that.medicalType = 2;
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/scss/outpatient/outpatient.scss";
</style>
